<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Log;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cookie;

//用户操作的控制器

class User extends Controller
{
    //登录
    public function login(Request $req){
        //查看登录功能是否打开
        if(!DB::table('config')->where('mark','login')->where('status',1)->first()){
            //登录功能已关闭
            return ['code'=>403,'message'=>'登录功能已关闭'];
        }

        $user = $req->user;
        $pwd = $req->pwd;
        $key = Cookie::get('key');
        if($user==null && $key != null){
            //判断key是否正确
            $user = DB::table('user')->where('id',$key)->first(['id','account','email']);
            if($user){
                //添加日志
                Log::save($user->id,'登录');
                //更新一下登录记录
                DB::table('user')->where('id',$user->id)->update(['login_time'=>date('Y-m-d H:i:s')]);
                //正常允许登录
                return ['code'=>200,'message'=>'登录成功','data'=>$user];
            }else{
                return ['code'=>-2,'message'=>'登录失败'];
            }
        }
        if($pwd == null){
            return ['code'=>-1,'message'=>'参数缺失'];
        }
        //进行登录判断
        $user = DB::table('user')->where('account',$user)->where('pwd',$pwd)->first(['id','account','email']);
        if($user){
            //这里应该设置cookie
            Cookie::queue('key', $user->id, 60*24); // 一天
            //添加日志
            Log::save($user->id,'登录');
            //更新一下登录记录
            DB::table('user')->where('id',$user->id)->update(['login_time'=>date('Y-m-d H:i:s')]);
            return ['code'=>200,'message'=>'登录成功','data'=>$user];
        }else{
            return ['code'=>-2,'message'=>'登录失败'];
        }
    }

    //注册
    public function register(Request $req){
        //查看注册功能是否打开
        if(!DB::table('config')->where('mark','register')->where('status',1)->first()){
            //注册功能已关闭
            return ['code'=>403,'message'=>'登录功能已关闭'];
        }

        $user = $req->user;
        $pwd = $req->pwd;
        $email = $req->email;
        if($user == null  || $pwd == null || $email==null){
            return ['code'=>-1,'message'=>'参数缺失'];
        }
        //查询下帐号是否存在
        if(DB::table('user')->where('account',$user)->first()){
            return ['code'=>-2,'message'=>'帐号已注册'];
        }
        $userdata = ['account'=>$user,'pwd'=>$pwd,'email'=>$email];
        //开始注册
        $user_id = DB::table('user')->insertGetId($userdata);
        if($user_id){
            //添加日志
            Log::save($user_id,'注册');
            return ['code'=>200,'message'=>'注册成功'];
        }else{
            return ['code'=>-1,'message'=>'注册失败'];
        }
    }

    //修改用户信息
    public function updateUser(Request $req){
        $user = $req->user;
        $data = [
            'pwd'=>$user['newpwd'],
            'email'=>$user['email'],
        ];
        //判断密码是否正确
        if(DB::table('user')->where('id',$user['id'])->where('pwd',$user['pwd'])->update($data)){
            return ['code'=>200,'message'=>'修改成功'];
        }else{
            return ['code'=>-1,'message'=>'原密码错误'];
        }


    }
}

